import java.io.IOException;
import java.util.*;

/**
 * @author soffo
 *
 */
public class VideoStoreFacade {

	VideoCopyController videoController = new VideoCopyController();
	RentalController rentalController = new RentalController();
	VideoCopyModel copyModel = new VideoCopyModel();
	RentalModel rentalModel = new RentalModel();
	CustomerModel customerModel = new CustomerModel();
	Scanner scanner = new Scanner(System.in);
	int option = 0;
	VideoCopyFileManager copyFileManager = new VideoCopyFileManager();

	/**
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public VideoStoreFacade() throws ClassNotFoundException, IOException {
		System.out
				.println("===========================Video Store System========================");
		System.out.println("1. Rent A Video");
		System.out.println("2. Maintain Video Data");
		option = scanner.nextInt();
		switch (option) {
		case 1:
			RentalsMenu();
			break;
		case 2:
			VideoCopyMenu();
			break;
		default:
			break;
		}
	}

	/**
	 * @throws ClassNotFoundException
	 * @throws IOException
	 * @throws InterruptedException
	 */
	/**
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public void VideoCopyMenu() throws ClassNotFoundException, IOException {
		ArrayList<VideoCopyModel> copyModels = new ArrayList<VideoCopyModel>();
		System.out.println("Video Copy Menu");
		System.out.println("1. add A video Copy");
		System.out.println("2. update A video Copy");
		System.out.println("3. delete A video Copy");
		System.out.println("4. Search A video Copy");
		option = scanner.nextInt();
		String flag = "YES";
		switch (option) {
		case 1:
			do {
				try {

					scanner = new Scanner(System.in);
					copyModel = new VideoCopyModel();
					System.out.println("Please Enter Title of the video copy");
					copyModel.setTitle(scanner.next());
					System.out
							.println("Please Enter Category of the video copy");
					copyModel.setCategory(scanner.next());
					System.out.println("Please Enter the video copy's number");
					copyModel.setVideoCopyNumber(scanner.nextInt());
					System.out
							.println("Please Enter the number of the video copies");
					copyModel.setNumOfCopies(scanner.nextInt());
					System.out.println("video copy checked out ?");
					copyModel.setCheckOut(scanner.nextBoolean());
					System.out.println("Please Enter the price");
					copyModel.setPrice(scanner.nextDouble());
					copyModels.add(copyModel);
					videoController = new VideoCopyController();
					videoController.addVideoCopy(copyModels);
					goToMain();
					System.out
							.println("Do you want to add another video copy: YES/NO");
					flag = scanner.next().toString();
				} catch (Exception e) {

					System.out.println("Please Enter valid data");
					flag = "yes";
					System.out.println("Please Renter The Record");

				}

			} while (flag.equalsIgnoreCase("YES"));

			break;
		case 2:
			do {
				try {

					System.out.println(updateVideoSubMenu());
					goToMain();
					System.out
							.println("Do you want to update another video copy: YES/NO");
					flag = scanner.next();
				} catch (Exception e) {
					System.out.println("Please Enter valid data");
					flag = "yes";
					System.out.println("Please Renter The Video Copy Number");
				}

			} while (flag.equalsIgnoreCase("YES"));

			break;
		case 3:

			do {
				try {
					scanner = new Scanner(System.in);
					copyFileManager = new VideoCopyFileManager();
					System.out
							.println("Please Enter the Video Copy Number you want to delete");
					int copyNumber1 = scanner.nextInt();
					videoController.DeleteVideoCopy(copyNumber1);
					ArrayList<VideoCopyModel> arrayList = copyFileManager
							.readFromFile();
					for (VideoCopyModel videoCopyModel : arrayList) {
						System.out.println(videoCopyModel);
					}
					goToMain();
					System.out
							.println("Do you want to delete another video copy: YES/NO");
					flag = scanner.next().toString();
				} catch (Exception e) {
					System.out.println("Please Enter valid data");
					flag = "yes";
					System.out.println("Please Renter The Video Copy Number");
				}

			} while (flag.equalsIgnoreCase("YES"));

			break;
		case 4:
			do {
				try {
					scanner = new Scanner(System.in);
					System.out
							.println("Please Enter the Video Copy Number you are searching for");
					int copyNumber = scanner.nextInt();
					videoController.SearchVideoCopy(copyNumber);
					goToMain();
					System.out
							.println("Do you want to search another video copy: YES/NO");
					flag = scanner.next().toString();
				} catch (Exception e) {
					System.out.println("Please Enter valid data");
					flag = "yes";
					System.out.println("Please Renter The Video Copy Number");
				}

			} while (flag.equalsIgnoreCase("YES"));

			break;
		default:

			break;

		}

	}

	/**
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public void RentalsMenu() throws ClassNotFoundException, IOException {
		System.out.println("Rentals Menu");
		System.out.println("1. Rent a Video");
		option = scanner.nextInt();
		String flag = "YES";
		switch (option) {
		case 1:
			do {
				try {
					scanner = new Scanner(System.in);
					copyModel = new VideoCopyModel();
					System.out.println("Please Enter the video copy's number");
					copyModel.setVideoCopyNumber(scanner.nextInt());
					System.out.println("Please Enter Title of the video copy");
					copyModel.setTitle(scanner.next());
					System.out.println("Please Enter the video copy's price");
					copyModel.setPrice(scanner.nextDouble());
					rentalModel.setVideoCopyModel(copyModel);
					System.out.println("Please Enter customer number");
					customerModel.setCustomerNumber(scanner.nextInt());
					System.out.println("Please Enter customer name");
					customerModel.setCustomerName(scanner.next());
					rentalModel.setCustomerModel(customerModel);

					ArrayList<RentalModel> rentalModels = new ArrayList<RentalModel>();
					rentalModels.add(rentalModel);
					rentalController.addRental(rentalModels);
					goToMain();
					System.out
							.println("Do you want to rent another video copy: YES/NO");
					flag = scanner.next();

				} catch (Exception e) {
					System.out.println("Please Enter valid data");
					flag = "yes";
					System.out.println("Please Renter The Record");
				}

			} while (flag.equalsIgnoreCase("YES"));
			/*
			 * System.out.println("Go To Main: YES/NO"); String op =
			 * scanner.next(); if (op.equalsIgnoreCase("yes")) {
			 * VideoStoreFacade facade = new VideoStoreFacade(); }
			 */
			break;
		}

	}

	/**
	 * @return
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public VideoCopyModel updateVideoSubMenu() throws ClassNotFoundException,
			IOException {

		scanner = new Scanner(System.in);
		int i = 0;
		ArrayList<VideoCopyModel> arrayList1 = copyFileManager.readFromFile();
		for (VideoCopyModel videoCopyModel : arrayList1) {
			System.out.println(videoCopyModel);
		}
		System.out
				.println("Please Enter the Video Copy Number you want to Update");
		int copyNumber12 = scanner.nextInt();
		VideoCopyModel copyModel = new VideoCopyModel();
		ArrayList<VideoCopyModel> arrayList2 = copyModel.findMovieByNumber(
				copyNumber12, arrayList1);
		for (VideoCopyModel videoCopyModel : arrayList2) {
			System.out.println(videoCopyModel);
			System.out
					.println("Please enter the column name you are willing to update");
			String column = scanner.next();
			if (column.equalsIgnoreCase("title")) {
				System.out
						.println("Please Enter the new value for the 'title column'");
				String title = scanner.next();
				videoCopyModel.setTitle(title);
			} else if (column.equalsIgnoreCase("category")) {
				System.out
						.println("Please Enter the new value for the 'category column'");
				String category = scanner.next();
				videoCopyModel.setCategory(category);
			}

			else if (column.equalsIgnoreCase("checkOut")) {
				System.out
						.println("Please Enter the new value for the 'checkOut column'");
				Boolean checkOut = scanner.nextBoolean();
				videoCopyModel.setCheckOut(checkOut);
			}

			else if (column.equalsIgnoreCase("numofcopies")) {
				System.out
						.println("Please Enter the new value for the 'numberOfCopies column'");
				Integer numOfCopies = scanner.nextInt();
				videoCopyModel.setNumOfCopies(numOfCopies);
			}

			else if (column.equalsIgnoreCase("price")) {
				System.out
						.println("Please Enter the new value for the 'price column'");
				Double price = scanner.nextDouble();
				videoCopyModel.setPrice(price);
			} else {
				System.out.println("Please Enter a valid column name");
			}
			arrayList1.set(i, videoCopyModel);
			copyFileManager.updateFile(arrayList1);
			i++;
			return videoCopyModel;

		}
		return null;
	}

	/**
	 * @throws ClassNotFoundException
	 * @throws IOException
	 */
	public void goToMain() throws ClassNotFoundException, IOException {
		System.out.println("Go To Main: YES/NO");
		String op = scanner.next();
		if (op.equalsIgnoreCase("yes")) {
			VideoStoreFacade facade = new VideoStoreFacade();
		}
	}
}
